import threading
import time

class TimeStatusKeeper:
    def __init__(self):
        self.status = 0  # home
        self.circle_flag = 1
        self.timer = None
        self.TURNING_LEFT = 123
        self.TURNING_RIGHT = 321
        self.home = 0
        self.GO_HOME = 666
        
    def set_status(self, seconds):
        """设置状态并启动定时器"""
        # 确保之前的定时器被取消
        if self.timer and self.timer.is_alive():
            self.timer.cancel()
        
        self.timer = threading.Timer(seconds, self._change_status)
        self.timer.start()
 
    def _change_status(self):
        """内部方法：修改状态到目标值"""
        if self.circle_flag == 1 and self.status == self.home:  # 第一圈
            self.status = self.TURNING_LEFT
        elif self.circle_flag == 2 and self.status == self.home:  # 第二圈
            self.status = self.TURNING_RIGHT
        elif self.status == self.TURNING_LEFT:
            self.status = self.GO_HOME
        elif self.status == self.TURNING_RIGHT:
            self.status = self.GO_HOME
        elif self.status == self.GO_HOME:
            self.status = self.home
            self.circle_flag = 2

        print(f"状态已切换至: {self.status}")

if __name__ == "__main__":
    keeper = TimeStatusKeeper()
    
    # # 定义目标变量和状态参数
    # my_target = "Initial"
    # OriginStatus = "Initial"
    # TargetStatus = "Updated"
    
    # # 设置状态保持 3 秒后切换
    # keeper.set_status(seconds=3, target=my_target, OriginStatus=OriginStatus, targetStatus=TargetStatus)
        
    # # 模拟主线程继续工作（非阻塞）
    # for i in range(1, 4):
    #     time.sleep(1)
    #     print(f"主线程运行中... [{i}/3]")
    
    # # 等待状态切换完成
    # time.sleep(0.5)
    # print(f"最终状态: {keeper.status}")